package com.muyu.system.danmo.mq;

import cn.hutool.json.JSONUtil;
import com.muyu.system.danmo.dto.DelayQueryPaymentInfo;
import com.muyu.system.danmo.dto.PaymentCallBackResult;
import com.muyu.system.danmo.service.PayChannelService;
import com.muyu.system.danmo.service.PayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.muyu.common.core.constant.TopicConstant.COMMON_TOPIC_DELAY;
import static com.muyu.system.danmo.util.ConsumerGroupConstant.PAY_DELAY_GROUP;
import static com.muyu.system.danmo.util.TagConstant.PAY_DELAY_TAG;


/**
 * 支付的状态是进行中，之后延迟查询订单信息
 *
 * @author Administrator
 */
@Component
@Slf4j
@RequiredArgsConstructor
@RocketMQMessageListener(
        topic = COMMON_TOPIC_DELAY,
        consumerGroup = PAY_DELAY_GROUP,
        selectorExpression = PAY_DELAY_TAG
)
public class PaySelectMessageConsumer implements RocketMQListener<String> {

    private final Map<String,PayChannelService> payChannelServiceMap;

    private final PayService payService;

    @Override
    public void onMessage(String message) {
        DelayQueryPaymentInfo bean = JSONUtil.toBean(message, DelayQueryPaymentInfo.class);
        log.info("延迟查询订单信息参数：{}", message);
        // 查询订单状态
        PayChannelService payChannelService = payChannelServiceMap.get(bean.getPayChannel().getServiceName());
        PaymentCallBackResult paymentCallBackResult = payChannelService.queryOrder(bean.getMchCode(),bean.getOrderNo());
        // 更新订单状态
        paymentCallBackResult.setBizOrderNo(bean.getBizOrderNo());
        payService.completeOrder(paymentCallBackResult);

    }
}
